﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// Tab.cs:
//
// Controle que imita uma aba
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WBFSSync.AeroControls
{
    public partial class Tab : UserControl
    {
        //----------------------- Variáveis

        Image icon = null;

        String text = "";

        Int32 icon_draw_pos_x = 0;
        Int32 icon_draw_pos_y = 0;
        
        Int32 text_draw_pos_x = 0;
        Int32 text_draw_pos_y = 0;

        Boolean draw_icon = true;

        Boolean mouse_over = false;
        Boolean selected = false;

        public Image Icon { get { return icon; } set { icon = value; OnIconChanged(); } }
        public String Caption { get { return text; } set { text = value; Invalidate(); } }
        public Int32 CaptionTextWidth { get { return TextRenderer.MeasureText(text, Skin.IconTitleFont).Width; } }

        public Boolean DrawIcon { get { return draw_icon; } set { draw_icon = value; OnDrawIconChanged(); } }

        public Boolean Selected { get { return selected; } set { selected = value; OnSelectedChanged(); } }

        //----------------------- Rotinas

        public Tab()
        {
            InitializeComponent();

            CalculatePositions();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (!Skin.SkinReady) return;

            e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
            e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            if (selected)
            {
                e.Graphics.DrawImage(Skin.tab_horizontal_l_c, 0, 0, 2, 21);
                e.Graphics.DrawImage(Skin.tab_horizontal_m_c, 2, 0, Width - 4, 21);
                e.Graphics.DrawImage(Skin.tab_horizontal_r_c, Width - 2, 0, 2, 21);

                if (icon != null) e.Graphics.DrawImage(icon, icon_draw_pos_x, icon_draw_pos_y + 1, 16, 16);
                if (text.Length > 0) e.Graphics.DrawString(text, Skin.IconTitleFont, new SolidBrush(ForeColor), 
                    text_draw_pos_x, text_draw_pos_y - 1);
            }
            else
            {
                if (mouse_over)
                {
                    e.Graphics.DrawImage(Skin.tab_horizontal_l_o, 0, 0, 2, 19);
                    e.Graphics.DrawImage(Skin.tab_horizontal_m_o, 2, 0, Width - 4, 19);
                    e.Graphics.DrawImage(Skin.tab_horizontal_r_o, Width - 2, 0, 2, 19);
                }
                else
                {
                    e.Graphics.DrawImage(Skin.tab_horizontal_l, 0, 0, 2, 19);
                    e.Graphics.DrawImage(Skin.tab_horizontal_m, 2, 0, Width - 4, 19);
                    e.Graphics.DrawImage(Skin.tab_horizontal_r, Width - 2, 0, 2, 19);
                }

                if (icon != null) e.Graphics.DrawImage(icon, icon_draw_pos_x, icon_draw_pos_y, 16, 16);
                if (text.Length > 0) e.Graphics.DrawString(text, Skin.IconTitleFont, new SolidBrush(ForeColor), 
                    text_draw_pos_x, text_draw_pos_y);
            }

            base.OnPaint(e);
        }

        private void CalculatePositions()
        {
            try
            {
                if (draw_icon)
                {
                    icon_draw_pos_x = 4;
                    icon_draw_pos_y = (Height - 16) / 2;

                    text_draw_pos_x = icon_draw_pos_x + 18;
                    text_draw_pos_y = (Height - Skin.IconTitleFont.Height) / 2 - 1;
                }
                else
                {
                    text_draw_pos_x = 4;
                    text_draw_pos_y = (Height - Skin.IconTitleFont.Height) / 2 - 1;
                }
            }
            catch
            {
            }
        }

        protected override void OnClick(EventArgs e)
        {
            selected = true;
            this.Invalidate();
            base.OnClick(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            mouse_over = true;
            this.Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            mouse_over = false;
            this.Invalidate();
            base.OnMouseLeave(e);
        }

        private void OnIconChanged()
        {
            
        }

        private void OnSelectedChanged()
        {
            this.Invalidate();
        }

        private void OnDrawIconChanged()
        {
            CalculatePositions();
        }
    }
}
